/*
 * PagosContribuyentesFijosFrame.java
 *
 * Created on 13 de abril de 2008, 11:44 AM
 */
package org.pagos.contribuyentes;

import org.mybeans.Sesion;
 
 
import org.mybeans.ArregloPagoBeans;
import org.mybeans.ArregloPagoImpuestosBeans;
import org.mybeans.ArregloPagoNumeracionBeans;
import org.mybeans.ContribuyentesBeans;
import org.mybeans.MorosidadBeans;

import org.reportes.template.CentavosALetrasConverter;
import org.general.utilidades.NumberToLetterConverter;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;
import org.contribuyentes.ContribuyentesBusquedaFrame;

/**
 *
 * @author  avbravo
 */
public class ContribuyentesArreglosPagoAgregarFrame extends javax.swing.JFrame {

    boolean focoContribuyente = false;
    String reportSource;
    String reportDest;
    double TotalDebe = 0.0;
    ArregloPagoBeans arregloPagoBeans;
    ArregloPagoNumeracionBeans arregloPagoNumeracionBeans;
    ArregloPagoImpuestosBeans arregloPagoImpuestosBeans;
    Sesion sesion;
    ContribuyentesBeans contribuyentesBeans;
    MorosidadBeans morosidadBeans;
    int fila = -1;
    DefaultTableModel modelo;
    private int AnioRecibo;
    private int NumeroMes;
    String Cedula;
    String TipoId = "contribuyentes";
    int IdArregloPago;
    double MorosidadInicial;
    private String idRegimen;
    String sql;
    String rowIdRegimen;
    String rowRegimen;
    double rowDeuda;
    double rowPago;
    double rowSaldo;
    double rowExoneracion;
    String rowTipo;
    String rowDetalle;
    double rowValor;
    double rowMorosidad;
    String rowComentario;
    int rowMesesMorosos;
    int n;
    int paginas;

    /** Creates new form */
    public ContribuyentesArreglosPagoAgregarFrame() {
        initComponents();
        sesion = new Sesion();
        /*
         * Fondo
         */

         
         
         

        jPanelFondo.setOpaque(false);
        setContentPane(jPanelFondo);
        /*
         * fondo
         */

        arregloPagoBeans = new ArregloPagoBeans();
        arregloPagoNumeracionBeans = new ArregloPagoNumeracionBeans();
        arregloPagoImpuestosBeans = new ArregloPagoImpuestosBeans();

        contribuyentesBeans = new ContribuyentesBeans();
        morosidadBeans = new MorosidadBeans();

        java.util.Calendar ca = java.util.Calendar.getInstance();
        java.sql.Date mydate = new java.sql.Date(ca.getTimeInMillis());

        jFormattedTextFieldFecha.setValue(mydate);


        this.AnioRecibo = ca.get(java.util.Calendar.YEAR);
        NumeroMes = ca.get(java.util.Calendar.MONTH);

        sesion.setAnioRecibo(AnioRecibo);
        sesion.setNumeroMesRecibo(NumeroMes);
        sesion.setTipoId("contribuyentes");

        CrearNuevoNumeroArregloPago();
        //
        modelo = (DefaultTableModel) jXTable1.getModel();

        modelo.isCellEditable(1, 1);
        TableColumn columnCheckBox = jXTable1.getColumnModel().getColumn(0);
        columnCheckBox.setPreferredWidth(130);

        TableColumn columnId = jXTable1.getColumnModel().getColumn(1);
        columnId.setPreferredWidth(150);

        TableColumn columnRegimen = jXTable1.getColumnModel().getColumn(2);
        columnRegimen.setPreferredWidth(130);

        jTextFieldMorosidadInicial.setText("");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelFondo = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldCedula = new javax.swing.JTextField();
        jButtonBuscar = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jTextFieldContribuyente = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        jXTable1 = new org.jdesktop.swingx.JXTable();
        jLabel18 = new javax.swing.JLabel();
        jTextFieldMorosidadInicial = new javax.swing.JTextField();
        jButtonLimpiar = new javax.swing.JButton();
        jButtonRegresar2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jTextFieldMesesMorosos = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jTextFieldAbonoInicial = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        jTextFieldPalabrasAbonoInicial = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        jTextFieldMensualidad = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        jTextFieldPalabrasAbonoMensual = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaDescripcion = new javax.swing.JTextArea();
        jButtonGuardar = new javax.swing.JButton();
        jFormattedTextFieldFecha = new javax.swing.JFormattedTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldIdArregloPago = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("ARREGLO PAGO CONTRIBUYENTES");
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
                formWindowGainedFocus(evt);
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
            }
        });

        jPanelFondo.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanelFondo.setOpaque(false);

        jLabel4.setText("Cedula");

        jTextFieldCedula.setColumns(35);
        jTextFieldCedula.setToolTipText("");
        jTextFieldCedula.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldCedulaControlActionPerformed(evt);
            }
        });
        jTextFieldCedula.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextFieldCedulaKeyPressed(evt);
            }
        });

        jButtonBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/search.png"))); // NOI18N
        jButtonBuscar.setToolTipText("Buscar");
        jButtonBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBuscarActionPerformed(evt);
            }
        });

        jLabel5.setText("Contribuyente");

        jTextFieldContribuyente.setColumns(70);
        jTextFieldContribuyente.setEnabled(false);

        jScrollPane2.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        jScrollPane2.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        jXTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "Regimen", "Morosidad", "Estatus", "Comentario", "Meses"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Double.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, true, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jXTable1.setOpaque(false);
        jScrollPane2.setViewportView(jXTable1);

        jLabel18.setText("Total Deuda");

        jTextFieldMorosidadInicial.setEnabled(false);
        jTextFieldMorosidadInicial.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldMorosidadInicialActionPerformed(evt);
            }
        });

        jButtonLimpiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/escoba.png"))); // NOI18N
        jButtonLimpiar.setToolTipText("Limpiar");
        jButtonLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLimpiarActionPerformed(evt);
            }
        });

        jButtonRegresar2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/regresar.jpeg"))); // NOI18N
        jButtonRegresar2.setToolTipText("Regresar");
        jButtonRegresar2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRegresar2ActionPerformed(evt);
            }
        });

        jLabel1.setText("Solo se muestra el recargo y la morosidad no el valor del impuesto actual.");

        jLabel7.setText("Meses de morosidad");

        jTextFieldMesesMorosos.setEditable(false);
        jTextFieldMesesMorosos.setEnabled(false);

        jLabel3.setText("La suma adeudada en consecuencia de ");

        jLabel10.setText("Haciendo abono incial de");

        jTextFieldAbonoInicial.setText("0.00");
        jTextFieldAbonoInicial.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldAbonoInicialActionPerformed(evt);
            }
        });

        jLabel15.setText("en palabras");

        jTextFieldPalabrasAbonoInicial.setFont(new java.awt.Font("DejaVu Sans", 0, 10)); // NOI18N

        jLabel14.setText("Abonar la suma mensual de");

        jTextFieldMensualidad.setText("0.00");
        jTextFieldMensualidad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldMensualidadActionPerformed(evt);
            }
        });

        jLabel13.setText("en palabras");

        jTextFieldPalabrasAbonoMensual.setFont(new java.awt.Font("DejaVu Sans", 0, 10)); // NOI18N

        jPanel2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel6.setText("Descripcion");

        jTextAreaDescripcion.setColumns(20);
        jTextAreaDescripcion.setRows(5);
        jScrollPane1.setViewportView(jTextAreaDescripcion);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(jLabel6)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(48, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jButtonGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/save.png"))); // NOI18N
        jButtonGuardar.setToolTipText("Guardar");
        jButtonGuardar.setEnabled(false);
        jButtonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonGuardarActionPerformed(evt);
            }
        });

        jFormattedTextFieldFecha.setEditable(false);
        jFormattedTextFieldFecha.setToolTipText("Fecha");
        jFormattedTextFieldFecha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jFormattedTextFieldFechaActionPerformed(evt);
            }
        });

        jLabel8.setText("Fecha");

        jLabel2.setText("Numero Arreglo Pago");

        jTextFieldIdArregloPago.setEditable(false);
        jTextFieldIdArregloPago.setToolTipText("Numero de recibo");
        jTextFieldIdArregloPago.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldIdArregloPagoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelFondoLayout = new javax.swing.GroupLayout(jPanelFondo);
        jPanelFondo.setLayout(jPanelFondoLayout);
        jPanelFondoLayout.setHorizontalGroup(
            jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelFondoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addGroup(jPanelFondoLayout.createSequentialGroup()
                        .addComponent(jLabel18)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldMorosidadInicial, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(69, 69, 69)
                        .addComponent(jLabel7)
                        .addGap(18, 18, 18)
                        .addComponent(jTextFieldMesesMorosos, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(662, Short.MAX_VALUE))
                    .addGroup(jPanelFondoLayout.createSequentialGroup()
                        .addComponent(jLabel10)
                        .addGap(36, 36, 36)
                        .addComponent(jTextFieldAbonoInicial, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(811, Short.MAX_VALUE))
                    .addGroup(jPanelFondoLayout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(728, Short.MAX_VALUE))
                    .addGroup(jPanelFondoLayout.createSequentialGroup()
                        .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 1136, Short.MAX_VALUE)
                            .addGroup(jPanelFondoLayout.createSequentialGroup()
                                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addGroup(jPanelFondoLayout.createSequentialGroup()
                                        .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel5)
                                            .addComponent(jLabel4))
                                        .addGap(3, 3, 3)
                                        .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(jPanelFondoLayout.createSequentialGroup()
                                                .addComponent(jTextFieldCedula, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jButtonBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addComponent(jTextFieldContribuyente, javax.swing.GroupLayout.PREFERRED_SIZE, 341, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel2)
                                            .addComponent(jLabel8))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jFormattedTextFieldFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jTextFieldIdArregloPago, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 135, Short.MAX_VALUE))))
                                .addGap(405, 405, 405)))
                        .addGap(434, 434, 434))
                    .addGroup(jPanelFondoLayout.createSequentialGroup()
                        .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanelFondoLayout.createSequentialGroup()
                                .addComponent(jLabel15)
                                .addGap(18, 18, 18)
                                .addComponent(jTextFieldPalabrasAbonoInicial))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanelFondoLayout.createSequentialGroup()
                                .addComponent(jLabel14)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextFieldMensualidad, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanelFondoLayout.createSequentialGroup()
                                .addComponent(jLabel13)
                                .addGap(18, 18, 18)
                                .addComponent(jTextFieldPalabrasAbonoMensual, javax.swing.GroupLayout.PREFERRED_SIZE, 870, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap(201, Short.MAX_VALUE))))
            .addGroup(jPanelFondoLayout.createSequentialGroup()
                .addGap(55, 55, 55)
                .addComponent(jButtonGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(34, 34, 34)
                .addComponent(jButtonLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30)
                .addComponent(jButtonRegresar2, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(1253, Short.MAX_VALUE))
        );
        jPanelFondoLayout.setVerticalGroup(
            jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelFondoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButtonBuscar)
                            .addComponent(jTextFieldCedula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jFormattedTextFieldFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jLabel4))
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelFondoLayout.createSequentialGroup()
                        .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(jTextFieldContribuyente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(5, 5, 5)
                        .addComponent(jLabel1))
                    .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(jTextFieldIdArregloPago, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel3)
                .addGap(11, 11, 11)
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel7)
                        .addComponent(jLabel18)
                        .addComponent(jTextFieldMesesMorosos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jTextFieldMorosidadInicial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldAbonoInicial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel15)
                    .addComponent(jTextFieldPalabrasAbonoInicial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel14)
                    .addComponent(jTextFieldMensualidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(5, 5, 5)
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(jTextFieldPalabrasAbonoMensual, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButtonRegresar2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButtonGuardar, javax.swing.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE)
                    .addComponent(jButtonLimpiar, javax.swing.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE))
                .addContainerGap(32, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanelFondo, javax.swing.GroupLayout.PREFERRED_SIZE, 1180, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelFondo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButtonRegresar2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRegresar2ActionPerformed

    dispose();
}//GEN-LAST:event_jButtonRegresar2ActionPerformed

private void jTextFieldMorosidadInicialActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldMorosidadInicialActionPerformed
}//GEN-LAST:event_jTextFieldMorosidadInicialActionPerformed

private void jButtonLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLimpiarActionPerformed

    Limpiar();

}//GEN-LAST:event_jButtonLimpiarActionPerformed

private void jButtonBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBuscarActionPerformed

    ContribuyentesBusquedaFrame contribuyentesBusquedaFrame = new ContribuyentesBusquedaFrame();
    contribuyentesBusquedaFrame.setLocationRelativeTo(null);
    contribuyentesBusquedaFrame.setVisible(true);
    focoContribuyente = true;
}//GEN-LAST:event_jButtonBuscarActionPerformed

    private void ActualizarContribuyente() {
        try {
            CargarContribuyentes();
            LimpiarTable();
            CargarTabla();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "ActualizarContribuyente()\nError  " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
private void jTextFieldCedulaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldCedulaKeyPressed
}//GEN-LAST:event_jTextFieldCedulaKeyPressed

private void jTextFieldCedulaControlActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldCedulaControlActionPerformed
    ValidarCedula();


}//GEN-LAST:event_jTextFieldCedulaControlActionPerformed

    private void ValidarCedula() {
        try {
            LimpiarTable();

            TotalDebe = 0.0;

            this.Cedula = this.jTextFieldCedula.getText();
            sesion.setCedulaContribuyente(this.jTextFieldCedula.getText());
            if (contribuyentesBeans.Consultar(this.Cedula) == true) {
                CargarContribuyentes();
                CargarTabla();
            }

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "ValidarCedula()\nError " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowGainedFocus
    // TODO add your handling code here:
    try {

        if (focoContribuyente) {
            TotalDebe = 0.0;
            ActualizarContribuyente();
        }

        focoContribuyente = false;
    } catch (Exception ex) {
        sesion.MostrarMensajeError("Error ", "Boton Actualizar Impuestos()\n " + ex.getMessage().toString());
    }
}//GEN-LAST:event_formWindowGainedFocus

private void jTextFieldAbonoInicialActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldAbonoInicialActionPerformed
    // TODO add your handling code here:
    try {
        String sMorosidadInicial = this.jTextFieldMorosidadInicial.getText();
        if (sMorosidadInicial == null || sMorosidadInicial.equals("")) {
            JOptionPane.showMessageDialog(this, "La deuda total es cero\n No se puede hacer arreglo de pago", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jTextFieldCedula.requestFocus();
            return;
        }
        this.MorosidadInicial = Double.parseDouble(sMorosidadInicial);
        if (MorosidadInicial == 0) {
            JOptionPane.showMessageDialog(this, "La deuda total es cero\n No se puede hacer arreglo de pago", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jTextFieldCedula.requestFocus();
            return;
        }

        String res;
        String sAbonoInicial = this.jTextFieldAbonoInicial.getText();

        if (sAbonoInicial == null || sAbonoInicial.equals("")) {
            JOptionPane.showMessageDialog(this, "Debe indicar el abono inicial", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            //jTextFieldAbonoInicial.requestFocus();
            return;
        }
        sAbonoInicial = sAbonoInicial.trim();
        double AbonoInicial = Double.parseDouble(sAbonoInicial);
        if (AbonoInicial <= 0) {
            JOptionPane.showMessageDialog(this, "Debe indicar el abono inicial", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jTextFieldAbonoInicial.requestFocus();
            return;
        }
        if (AbonoInicial > MorosidadInicial) {
            JOptionPane.showMessageDialog(this, "El abono inicial no debe ser mayor que la morosidad inicial", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jTextFieldAbonoInicial.requestFocus();
            return;
        }

        res = NumberToLetterConverter.convertNumberToLetter(AbonoInicial);
        int pos = sAbonoInicial.indexOf('.');

        if (pos > 0) {
            int largo = sAbonoInicial.length();
            String sCentavos = sAbonoInicial.substring(pos + 1, largo);
            sCentavos = sCentavos.trim();
            double centavos = Double.parseDouble(sCentavos);
            if (centavos >= 50) {
                int entero = pos - 1;
                if (entero >= 0) {
                    String Sdolar = sAbonoInicial.substring(0, pos);
                    if (Sdolar == null) {
                        Sdolar = "0";
                    }
                    if (Sdolar == null) {
                        Sdolar = "0";
                    }


                    int dolar = Integer.parseInt(Sdolar);
                    // dolar--;
                    res = NumberToLetterConverter.convertNumberToLetter(dolar);
                }
            }
            String cen = CentavosALetrasConverter.convertNumberToLetter(centavos);
            res += " CON " + cen;

        }

        jTextFieldPalabrasAbonoInicial.setText(res);
    } catch (Exception ex) {
        sesion.MostrarMensajeError("Error ", "jTextFieldAbonoInicialActionPerformed()\n " + ex.getMessage().toString());
    }
}//GEN-LAST:event_jTextFieldAbonoInicialActionPerformed

private void jTextFieldMensualidadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldMensualidadActionPerformed
    // TODO add your handling code here:
    try {
        String sMorosidadInicial = this.jTextFieldMorosidadInicial.getText();
        if (sMorosidadInicial == null || sMorosidadInicial.equals("")) {
            JOptionPane.showMessageDialog(this, "La deuda total es cero\n No se puede hacer arreglo de pago", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jTextFieldCedula.requestFocus();
            return;
        }
        this.MorosidadInicial = Double.parseDouble(sMorosidadInicial);
        if (MorosidadInicial == 0) {
            JOptionPane.showMessageDialog(this, "La deuda total es cero\n No se puede hacer arreglo de pago", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jTextFieldCedula.requestFocus();
            return;
        }
        String res;
        String sMensualidad = this.jTextFieldMensualidad.getText();

        if (sMensualidad == null || sMensualidad.equals("")) {
            JOptionPane.showMessageDialog(this, "Debe indicar la mensualidad", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jTextFieldMensualidad.requestFocus();
            return;
        }
        double Mensualidad = Double.parseDouble(sMensualidad);
        if (Mensualidad <= 0) {
            JOptionPane.showMessageDialog(this, "Debe indicar la mensualidad", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jTextFieldMensualidad.requestFocus();
            return;
        }
        if (Mensualidad > MorosidadInicial) {
            JOptionPane.showMessageDialog(this, "El pago mensual no debe ser mayor que la deuda", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jTextFieldMensualidad.requestFocus();
            return;
        }

        /*
         * convierte numeros a letras
         */
        res = NumberToLetterConverter.convertNumberToLetter(Mensualidad);
        int pos = sMensualidad.indexOf('.');

        if (pos > 0) {
            int largo = sMensualidad.length();
            String sCentavos = sMensualidad.substring(pos + 1, largo);
            sCentavos = sCentavos.trim();
            double centavos = Double.parseDouble(sCentavos);
            if (centavos >= 50) {
                int entero = pos - 1;
                if (entero >= 0) {
                    String Sdolar = sMensualidad.substring(0, pos);
                    if (Sdolar == null) {
                        Sdolar = "0";
                    }
                    if (Sdolar == null) {
                        Sdolar = "0";
                    }


                    int dolar = Integer.parseInt(Sdolar);
                    // dolar--;
                    res = NumberToLetterConverter.convertNumberToLetter(dolar);
                }
            }
            String cen = CentavosALetrasConverter.convertNumberToLetter(centavos);
            res += " CON " + cen;

        }

        jTextFieldPalabrasAbonoMensual.setText(res);
    } catch (Exception ex) {
        sesion.MostrarMensajeError("Error ", "jTextFieldMensualidadActionPerformed()\n " + ex.getMessage().toString());
    }
}//GEN-LAST:event_jTextFieldMensualidadActionPerformed

private void jButtonGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonGuardarActionPerformed
    // TODO add your handling code here:

    GuardarArregloPago();
}//GEN-LAST:event_jButtonGuardarActionPerformed

private void jFormattedTextFieldFechaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jFormattedTextFieldFechaActionPerformed
}//GEN-LAST:event_jFormattedTextFieldFechaActionPerformed

private void jTextFieldIdArregloPagoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldIdArregloPagoActionPerformed
}//GEN-LAST:event_jTextFieldIdArregloPagoActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ContribuyentesArreglosPagoAgregarFrame().setVisible(true);
            }
        });
    }

    /*
     * 
     *verifica si ya se pago la placa para este año 
     * 
     */
    public void CargarTabla() {
        try {
            jButtonGuardar.setEnabled(false);
            arregloPagoBeans.setId(Cedula);
            arregloPagoBeans.setTipoId("contribuyentes");
            arregloPagoBeans.setAnio(this.AnioRecibo);
            if (arregloPagoBeans.TieneArregloPagoSinCancelar() == true) {
                JOptionPane.showMessageDialog(null, "Tiene un arreglo de pagos anterior sin cancelar.\n Debera cancelar ese primero. ", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            jButtonGuardar.setEnabled(true);
            /*
            el parametro false indica que no se comprobara las dependencias que tiene (vehiculos y remolques)
             * ya que el arreglo de pago es de los impuestos que tiene como contribueyente.
             */
              morosidadBeans.setAnio(this.AnioRecibo);
            String lmes = sesion.getNombreMes(NumeroMes);
            morosidadBeans.setMes(lmes);
            morosidadBeans.VerificarMorosidad(this.Cedula, "contribuyentes", false);
            if (morosidadBeans.getNumeroRegistrosLista() == -1) {
                JOptionPane.showMessageDialog(this, "Este contribuyente no tiene morosidad", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            AgregarFila();

            /*
             this.sql = "select recargomensual.*, regimen.regimen "
                    + "from recargomensual, regimen "
                    + " where recargomensual.idmunicipio ='" + sesion.getIdmunicipio() + "'"
                    + " and  recargomensual.id ='" + this.Cedula + "'"
                    + " and  recargomensual.tipoid ='contribuyentes'"
                    + " and  recargomensual.anio='" + this.AnioRecibo + "'"
                    + " and  recargomensual.recargoactual > 0 and  recargomensual.idmunicipio = regimen.idmunicipio "
                    + " and  recargomensual.idregimen = regimen.idregimen  "
                    + " and  regimen.activo='si'";
              recargoMensualBeans.CargarListadoRecargoMensual(this.sql);
            */
            this.jXTable1.setModel(modelo);
            jXTable1.setVisible(true);

            this.jXTable1.addMouseListener(new MouseAdapter() {

                public void mouseClicked(MouseEvent e) {
                    if (SwingUtilities.isRightMouseButton(e)) {
//                        popupMenu.show(jXTable1, e.getX(), e.getY());
                    }

                    fila = jXTable1.rowAtPoint(e.getPoint());
                    int columna = jXTable1.columnAtPoint(e.getPoint());
                    if ((fila > -1) && (columna > -1)) {
                    }
                }
            });



        } catch (Exception ex) {

            sesion.MostrarMensajeError("Error ", "CargarTabla()\n " + ex.getMessage().toString());
        }

    }

    public void CargarContribuyentes() {
        try {

            if (contribuyentesBeans.getActivo().equals("no")) {
                JOptionPane.showMessageDialog(this, "Este registro esta inactivo", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            this.jTextFieldCedula.setText(contribuyentesBeans.getCedula());
            sesion.setCedulaContribuyente(this.jTextFieldCedula.getText());
            this.jTextFieldContribuyente.setText(contribuyentesBeans.getContribuyente());
            this.Cedula = this.jTextFieldCedula.getText();
            sesion.setId(this.jTextFieldCedula.getText());
            repaint();


        } catch (Exception ex) {
            sesion.MostrarMensajeError("Error ", "CargarContribuyentes()\n " + ex.getMessage().toString());
        }
    }

    private void Limpiar() {
        try {
            jTextFieldMorosidadInicial.setText("");
            LimpiarContribuyentes();
            LimpiarTable();

            CrearNuevoNumeroArregloPago();
            jTextAreaDescripcion.setText("");
            jButtonGuardar.setEnabled(false);
        } catch (Exception ex) {
            sesion.MostrarMensajeError("Error ", "Limpiar()\n " + ex.getMessage().toString());
        }
    }

    private void LimpiarContribuyentes() {
        jTextFieldCedula.setText("");
        jTextFieldContribuyente.setText("");
    }

    @SuppressWarnings("static-access")
    private void AgregarFila() {
        try {
            Object[] filas = new Object[6]; // uno mas de la cantidad
            int mayor = 0;
            System.out.println("ax numero registros " +morosidadBeans.getNumeroRegistrosLista());
            for (int m = 0; m <= morosidadBeans.getNumeroRegistrosLista(); m++) {
     
                rowIdRegimen = morosidadBeans.getListaIdRegimen().get(m);
                System.out.println("ax idRegimen " + rowIdRegimen);
                rowRegimen = morosidadBeans.getListaRegimen().get(m);
                rowDeuda = morosidadBeans.getListaMorosidad().get(m);
                rowTipo = morosidadBeans.getListaEstatus().get(m);
                rowTipo = rowTipo.trim();
                if(rowTipo.equals("morosidadanual")){
                    rowComentario="";
                      rowMesesMorosos = 0;
                }else{
                rowComentario = morosidadBeans.getListaComentario().get(m);
                rowMesesMorosos = morosidadBeans.getListaMesesMorosos().get(m);
                }
                

                
                filas[0] = rowIdRegimen;
                filas[1] = rowRegimen;
                filas[2] = rowDeuda;

                filas[3] = rowTipo;
                filas[4] = rowComentario;
                filas[5] = rowMesesMorosos;
                if (rowMesesMorosos > mayor) {
                    mayor = rowMesesMorosos;
                }
                modelo.addRow(filas);
            }//for
            this.TotalDebe = morosidadBeans.getTotalMorosidad();
            jTextFieldMorosidadInicial.setText(String.valueOf(this.TotalDebe));
            jTextFieldMesesMorosos.setText(String.valueOf(mayor));
        } catch (Exception ex) {
            System.out.println("ex " +ex.getCause());
            sesion.MostrarMensajeError("Error ", "AgregarFila()\n " + ex.getMessage().toString());
        }
    }

    private void LimpiarTable() {
        try {
            //limpiar  el jXTable

            boolean control = false;
            while (control == false) {
                int n = jXTable1.getRowCount();
                //System.out.println("n" + n);
                if (n <= 0) {
                    control = true;
                } else {
                    modelo.removeRow(0);
                }

            }//while
            jTextFieldMorosidadInicial.setText("");
            jTextFieldMesesMorosos.setText("");
        } catch (Exception ex) {
            sesion.MostrarMensajeError("Error ", "LimpiarTable()\n " + ex.getMessage().toString());
        }
    }

    private void GuardarArregloPago() {
        try {
            String Contribuyente = this.jTextFieldContribuyente.getText();
            if (Contribuyente == null || Contribuyente.equals("")) {
                JOptionPane.showMessageDialog(this, "No ha seleccionado ningun Contribuyente", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                jTextFieldCedula.requestFocus();
                return;
            }
            String Cedula = this.jTextFieldCedula.getText();
            if (Cedula == null || Cedula.equals("")) {
                JOptionPane.showMessageDialog(this, "No ha seleccionado ningun Contribuyente", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                jTextFieldCedula.requestFocus();
                return;
            }
            String SIdArregloPago = this.jTextFieldIdArregloPago.getText();
            if (SIdArregloPago == null || SIdArregloPago.equals("")) {
                JOptionPane.showMessageDialog(this, "No se ha generado ningun numero de arreglo de pago", "Mensaje", JOptionPane.WARNING_MESSAGE);
                jTextFieldCedula.requestFocus();
                return;
            }
            this.IdArregloPago = Integer.parseInt(SIdArregloPago);
            if (this.IdArregloPago <= 0) {
                JOptionPane.showMessageDialog(this, "No se ha generado ningun numero de arreglo de pago", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                jTextFieldCedula.requestFocus();
                return;
            }

            String sMorosidadInicial = this.jTextFieldMorosidadInicial.getText();
            if (sMorosidadInicial == null || sMorosidadInicial.equals("")) {
                JOptionPane.showMessageDialog(this, "La deuda total es cero\n No se puede hacer arreglo de pago", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                jTextFieldCedula.requestFocus();
                return;
            }
            this.MorosidadInicial = Double.parseDouble(sMorosidadInicial);
            if (MorosidadInicial == 0) {
                JOptionPane.showMessageDialog(this, "La deuda total es cero\n No se puede hacer arreglo de pago", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                jTextFieldCedula.requestFocus();
                return;
            }

            String sMesesMorosos = this.jTextFieldMesesMorosos.getText();

            if (sMesesMorosos == null || sMesesMorosos.equals("")) {
                JOptionPane.showMessageDialog(this, "El total de meses morosos es cero\n No se puede hacer arreglo de pago", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                jTextFieldCedula.requestFocus();
                return;
            }
            int MesesMorosos = Integer.parseInt(sMesesMorosos);
            if (MesesMorosos == 0) {
                JOptionPane.showMessageDialog(this, "El total de meses morosos no debe ser cero.\n si aparece cero puede ser que se trate de recargo\n o vigencias expiradas o de morosidad anual\nEste es solo un  mensaje informativo", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                // jTextFieldCedula.requestFocus();
                // return;
            }


            String sFecha = jFormattedTextFieldFecha.getText();
            if (sFecha == null || sFecha.equals("")) {
                JOptionPane.showMessageDialog(this, "seleccione la fecha ", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                jFormattedTextFieldFecha.requestFocus();
            }

            n = jXTable1.getRowCount();
            if (n == -1) {
                JOptionPane.showMessageDialog(this, "No se ha cargado ninguna deuda", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }

            String sAbonoInicial = this.jTextFieldAbonoInicial.getText();
            if (sAbonoInicial == null || sAbonoInicial.equals("")) {
                JOptionPane.showMessageDialog(this, "Debe indicar el abono inicial", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                jTextFieldAbonoInicial.requestFocus();
                return;
            }
            double AbonoInicial = Double.parseDouble(sAbonoInicial);
            if (AbonoInicial <= 0) {
                JOptionPane.showMessageDialog(this, "Debe indicar el abono inicial", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                jTextFieldAbonoInicial.requestFocus();
                return;
            }
            if (AbonoInicial > MorosidadInicial) {
                JOptionPane.showMessageDialog(this, "El abono inicial no debe ser mayor que la morosidad inicial", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                jTextFieldAbonoInicial.requestFocus();
                return;
            }

            String PalabrasAbonoInicial = this.jTextFieldPalabrasAbonoInicial.getText();
            if (PalabrasAbonoInicial == null || PalabrasAbonoInicial.equals("")) {
                JOptionPane.showMessageDialog(this, "Debe indicar el abono inicial\n y presionar la tecla enter \n para generar la cantidad en palabras", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                jTextFieldAbonoInicial.requestFocus();
                return;
            }


            String sMensualidad = this.jTextFieldMensualidad.getText();
            if (sMensualidad == null || sMensualidad.equals("")) {
                JOptionPane.showMessageDialog(this, "Debe indicar la mensualidad", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                jTextFieldMensualidad.requestFocus();
                return;
            }
            double Mensualidad = Double.parseDouble(sMensualidad);
            if (Mensualidad <= 0) {
                JOptionPane.showMessageDialog(this, "Debe indicar la mensualidad", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                jTextFieldMensualidad.requestFocus();
                return;
            }
            if (Mensualidad > MorosidadInicial) {
                JOptionPane.showMessageDialog(this, "El pago mensual no debe ser mayor que la deuda", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                jTextFieldMensualidad.requestFocus();
                return;
            }

            String PalabrasAbonoMensual = this.jTextFieldPalabrasAbonoMensual.getText();
            if (PalabrasAbonoMensual == null || PalabrasAbonoMensual.equals("")) {
                JOptionPane.showMessageDialog(this, "Debe indicar el abono mensual\n y presionar la tecla enter \n para generar la cantidad en palabras", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                jTextFieldMensualidad.requestFocus();
                return;
            }
            String Descripcion = this.jTextAreaDescripcion.getText();
            if (Descripcion == null || Descripcion.equals("")) {
                JOptionPane.showMessageDialog(this, "Debe indicar una descripcion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                jTextAreaDescripcion.requestFocus();
                return;
            }

            arregloPagoBeans.setIdArregloPago(IdArregloPago);
            arregloPagoBeans.setId(Cedula);
            arregloPagoBeans.setTipoId("contribuyentes");
            arregloPagoBeans.setAnio(this.AnioRecibo);
            java.util.Date myFecha = (java.util.Date) jFormattedTextFieldFecha.getValue();
            java.sql.Date date = new java.sql.Date(myFecha.getTime());
            arregloPagoBeans.setFechaArregloPago(date);
            arregloPagoBeans.setMorosidadInicial(MorosidadInicial);
            arregloPagoBeans.setMesesMorosos(MesesMorosos);
            arregloPagoBeans.setAbonoInicial(AbonoInicial);
            arregloPagoBeans.setPalabrasAbonoInicial(PalabrasAbonoInicial);
            arregloPagoBeans.setMensualidad(Mensualidad);
            arregloPagoBeans.setPalabrasAbonoMensual(PalabrasAbonoMensual);
//la primera vez el saldo actual sera igual a la morosidad inicial
            arregloPagoBeans.setSaldoActual(MorosidadInicial);
            arregloPagoBeans.setDescripcion(Descripcion);
            arregloPagoBeans.setFecha(date);
            if (arregloPagoBeans.Guardar() == false) {
                JOptionPane.showMessageDialog(null, "No se pudo guardar  el arreglo de pago\nIntentelo nuevamente...", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }

            /*
             * Guardo la numeracion del arreglo de pago
             */
            arregloPagoNumeracionBeans.setActivo("si");
            arregloPagoNumeracionBeans.setIdMunicipio(sesion.getIdmunicipio());
            arregloPagoNumeracionBeans.setIdArregloPago(IdArregloPago);
            boolean guardado = arregloPagoNumeracionBeans.Guardar();
            if (guardado == false) {
                sesion.MostrarMensajeAdvertencia("Advertencia", "No se pudo guardar la numeracion del recibo\nIntentelo nuevamente");
                return;
            }

            /*
             *
             * Guardo los impuestos del arreglo de pago
             *
             */


            int n = jXTable1.getRowCount();
            if (n < 0) {
                JOptionPane.showMessageDialog(this, "Numero de filas negativo ", "Error", JOptionPane.ERROR_MESSAGE);
                return;
            }
            arregloPagoImpuestosBeans.setActivo("si");
            arregloPagoImpuestosBeans.setFecha(date);
            arregloPagoImpuestosBeans.setIdArregloPago(IdArregloPago);
            for (int i = 0; i < n; i++) {
                String idRegimen = String.valueOf(jXTable1.getValueAt(i, 0));

                String SDeuda = String.valueOf(jXTable1.getValueAt(i, 2));
                double Deuda = 0.0;
                if (SDeuda == null || SDeuda.equals("")) {
                    Deuda = 0.0;
                } else {
                    Deuda = Double.parseDouble(SDeuda);
                }
                int mesesMorosos = 0;
                String SMesesMorosos = String.valueOf(jXTable1.getValueAt(i, 5));
                if (SMesesMorosos == null || SMesesMorosos.equals("")) {
                    mesesMorosos = 0;
                } else {
                    mesesMorosos = Integer.parseInt(SMesesMorosos);
                }
                String Estatus = String.valueOf(jXTable1.getValueAt(i, 3));
                if (Estatus == null || Estatus.equals("")) {
                    Estatus = "";
                }
                String Comentario = String.valueOf(jXTable1.getValueAt(i, 4));
                if (Comentario == null || Comentario.equals("")) {
                    Comentario = "";
                }

                arregloPagoImpuestosBeans.setMesesmorosos(mesesMorosos);
                arregloPagoImpuestosBeans.setDeuda(Deuda);
                arregloPagoImpuestosBeans.setIdRegimen(idRegimen);
                arregloPagoImpuestosBeans.setEstatus(Estatus);
                arregloPagoImpuestosBeans.setComentario(Comentario);
                arregloPagoImpuestosBeans.Guardar();

            }




            jButtonGuardar.setEnabled(false);
            JOptionPane.showMessageDialog(null, "Se guardo exitosamente el arreglo de pago\nPrepare la impresora para imprimir el recibo\n con hojas 8 1/2 x 11 y presione el boton Aceptar", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            ImprimirRecibo();
            CrearNuevoNumeroArregloPago();

        } catch (Exception ex) {

            sesion.MostrarMensajeError("Error ", "GuardarArregloPago()\n " + ex.getMessage().toString());
        }
    }

    private void ImprimirRecibo() {
        try {
            reportSource = sesion.getRutaReportes() + "ArregloPagoContribuyentes.jrxml";
            Map<String, Object> params = new HashMap<String, Object>();


            if (sesion.Conectar() == false) {
                JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            JasperReport jasperReport =
                    JasperCompileManager.compileReport(reportSource);
            params.put("P_IDARREGLOPAGO", this.IdArregloPago);
            params.put("P_IDMUNICIPIO", sesion.getIdmunicipio());
            params.put("P_MUNICIPIO", sesion.getMunicipio());
            params.put("P_NOMBREUSUARIO", sesion.getUsuario());
//        
            JasperPrint jasperPrint =
                    JasperFillManager.fillReport(
                    jasperReport, params, sesion.con);

            JasperViewer.viewReport(jasperPrint, false);
        } catch (Exception ex) {
            sesion.MostrarMensajeError("Error ", "ImprimirUnaPagina()\n " + ex.getMessage().toString());
        }
    }

    private void CrearNuevoNumeroArregloPago() {
        try {
            //   int NuevoNumeroRecibo = recibesBeans.getNumeroReciboDisponible();
            int NuevoNumeroArregloPago = arregloPagoNumeracionBeans.ArregloPagoUltimo();
            if (NuevoNumeroArregloPago != -1) {
                NuevoNumeroArregloPago++;
                jTextFieldIdArregloPago.setText(String.valueOf(NuevoNumeroArregloPago));

                jTextFieldAbonoInicial.setText("0.00");
                jTextFieldMensualidad.setText("0.00");
                jTextFieldMesesMorosos.setText("");
                jTextFieldCedula.setText("");
                jTextFieldContribuyente.setText("");
                jTextFieldPalabrasAbonoInicial.setText("");
                jTextFieldPalabrasAbonoMensual.setText("");
                jButtonGuardar.setEnabled(false);
            } else {
                sesion.MostrarMensajeInformacion("Mensaje", "Debe registrar la numeracion inicial de arreglos de pago\n Disponible en el menu Contabilidad\nNumeracion de arreglos de Pago\nCuando los cree puede utilizar esta opción.");
                //dispose();
            }

        } catch (Exception ex) {
            sesion.MostrarMensajeError("Error ", "CrearNuevoRecibo()\n " + ex.getMessage().toString());
        }

    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonBuscar;
    private javax.swing.JButton jButtonGuardar;
    private javax.swing.JButton jButtonLimpiar;
    private javax.swing.JButton jButtonRegresar2;
    private javax.swing.JFormattedTextField jFormattedTextFieldFecha;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanelFondo;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextAreaDescripcion;
    private javax.swing.JTextField jTextFieldAbonoInicial;
    private javax.swing.JTextField jTextFieldCedula;
    private javax.swing.JTextField jTextFieldContribuyente;
    private javax.swing.JTextField jTextFieldIdArregloPago;
    private javax.swing.JTextField jTextFieldMensualidad;
    private javax.swing.JTextField jTextFieldMesesMorosos;
    private javax.swing.JTextField jTextFieldMorosidadInicial;
    private javax.swing.JTextField jTextFieldPalabrasAbonoInicial;
    private javax.swing.JTextField jTextFieldPalabrasAbonoMensual;
    private org.jdesktop.swingx.JXTable jXTable1;
    // End of variables declaration//GEN-END:variables
}
